package com.mlizhi.modules.spec.setting;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.TextView;

import com.mlizhi.base.Session;
import com.mlizhi.base.imageloader.core.DisplayImageOptions;
import com.mlizhi.base.imageloader.core.ImageLoader;
import com.mlizhi.modules.login.LoginActivity;
import com.mlizhi.modules.spec.ISpecInterface;
import com.mlizhi.modules.splash.PreSettingActivity;
import com.mlizhi.kingdom.R;
import com.mlizhi.utils.Constants;
import com.umeng.analytics.MobclickAgent;

public class SpecSettingFragment extends Fragment {
    // 缓存Fragment view
    private View rootView;
    // ISpecInterface的实例，用来使SpecActivity做回调的
    private ISpecInterface specCallback;

    private Activity mContext;

    private TextView settingTv;
    private TextView userNickName;
    private View userInfoLy = null;
    private ImageView userInfoPhoto;
    private RadioGroup userSexGroup;
    private Session mSession;
    private TextView ageTextView;
    private TextView skinTextView;
    private TextView asycTextView;

    private static SpecSettingFragment specSettingFragment;
    private DisplayImageOptions displayImageOptions = null;

    String uid = null;

    public SpecSettingFragment() {

    }

		public static SpecSettingFragment getNewInstance() {
			if(specSettingFragment == null) {
				specSettingFragment = new SpecSettingFragment();
			}
			return specSettingFragment;
		}

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        this.mContext = activity;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mSession = Session.get(getActivity());
        uid = mSession.getUid();
        displayImageOptions = new DisplayImageOptions.Builder()
                .showImageOnLoading(R.drawable.ic_tourist)
                .showImageForEmptyUri(R.drawable.ic_tourist)
                .showImageOnFail(R.drawable.ic_tourist).cacheInMemory(true) // default
                .cacheOnDisk(true) // default
                .build();
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        if (rootView == null) {
            rootView = inflater.inflate(R.layout.fragment_spec_setting, container, false);
        }
        // 缓存的rootView需要判断是否已经被加过parent，
        // 如果有parent需要从parent删除，要不然会发生这个rootview已经有parent的错误。
        ViewGroup parent = (ViewGroup) rootView.getParent();
        if (parent != null) {
            parent.removeView(rootView);
        }
        settingTv = (TextView) rootView.findViewById(R.id.id_system_setting_label);
        userInfoLy = rootView.findViewById(R.id.id_user_info_ly);
        userSexGroup = (RadioGroup) rootView.findViewById(R.id.id_user_sex_ly);

        ageTextView = (TextView) rootView.findViewById(R.id.id_user_age_value);
        skinTextView = (TextView) rootView.findViewById(R.id.id_user_skin_value);
        asycTextView = (TextView) rootView.findViewById(R.id.id_user_asyc_value);


        userInfoPhoto = (ImageView) rootView.findViewById(R.id.id_cover_user_photo);
        userNickName = (TextView) rootView.findViewById(R.id.id_user_info_name);
        return rootView;
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        specCallback = (ISpecInterface) getActivity();
        settingTv.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                MobclickAgent.onEvent(mContext, "startSettingActivity");
                getActivity().startActivityForResult(new Intent(mContext, SpecSettingListActivity.class), Constants.SPEC_SETTING_LIST_REQUEST_CODE);
            }
        });
        userInfoLy.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                MobclickAgent.onEvent(mContext, "startMineActivity");
                if (uid == null || "".equals(uid)) {
                    Intent intent = new Intent(getActivity(), LoginActivity.class);
                    intent.putExtra(Constants.LOGIN_FROM_FLAG, SpecSettingFragment.class.getSimpleName());
                    getActivity().startActivityForResult(intent, Constants.SPEC_LOGIN_REQUEST_CODE);
                } else {
                    getActivity().startActivityForResult(new Intent(mContext, SpecSettingMineActivity.class), Constants.SPEC_SETTING_MINE_REQUEST_CODE);
                }
            }
        });


        ageTextView.setText(mSession.getUserBriday());
        if (Constants.USER_SEX_FEMALE.equals(mSession.getUserSex())) {
            userSexGroup.check(R.id.id_user_sex_female);
        } else if (Constants.USER_SEX_MALE.equals(mSession.getUserSex())) {
            userSexGroup.check(R.id.id_user_sex_male);
        }
        String skinType = getString(R.string.user_pre_setting_skin_type_unknow);
        if (Constants.USER_SKIN_TYPE_DRY.equals(mSession.getUserSkinType())) {
            skinType = getString(R.string.user_pre_setting_skin_type_dry);
        } else if (Constants.USER_SKIN_TYPE_OILY.equals(mSession.getUserSkinType())) {
            skinType = getString(R.string.user_pre_setting_skin_type_oil);
        } else if (Constants.USER_SKIN_TYPE_HYBRID.equals(mSession.getUserSkinType())) {
            skinType = getString(R.string.user_pre_setting_skin_type_hybird);
        } else if (Constants.USER_SKIN_TYPE_SENSITIVE.equals(mSession.getUserSkinType())) {
            skinType = getString(R.string.user_pre_setting_skin_type_sensitive);
        } else if (Constants.USER_SKIN_TYPE_UNKNOW.equals(mSession.getUserSkinType())) {
            skinType = getString(R.string.user_pre_setting_skin_type_unknow);
        }
        skinTextView.setText(skinType);


        ageTextView.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                if (uid == null || "".equals(uid)) {
                    Intent intent = new Intent(mContext, SpecSettingMineUserBridayActivity.class);
                    intent.putExtra(Constants.NO_SAVE_FLAG, Constants.NO_SAVE_FLAG);
                    getActivity().startActivityForResult(intent, Constants.REQUEST_NO_NET_REQUEST_CODE);
                } else {
                    getActivity().startActivityForResult(new Intent(mContext, SpecSettingMineActivity.class), Constants.SPEC_SETTING_MINE_REQUEST_CODE);
                }
            }
        });
        skinTextView.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                if (uid == null || "".equals(uid)) {
                    Intent intent = new Intent(mContext, SpecSettingMineUserSkinTypeActivity.class);
                    intent.putExtra(Constants.NO_SAVE_FLAG, Constants.NO_SAVE_FLAG);
                    getActivity().startActivityForResult(intent, Constants.REQUEST_NO_NET_REQUEST_CODE);
                } else {
                    getActivity().startActivityForResult(new Intent(mContext, SpecSettingMineActivity.class), Constants.SPEC_SETTING_MINE_REQUEST_CODE);
                }
            }
        });
        asycTextView.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {

                if (uid == null || "".equals(uid)) {
                    Intent intent = new Intent(getActivity(), LoginActivity.class);
                    intent.putExtra(Constants.LOGIN_FROM_FLAG, SpecSettingFragment.class.getSimpleName());
                    getActivity().startActivityForResult(intent, Constants.SPEC_LOGIN_REQUEST_CODE);
                } else {
                    getActivity().startActivityForResult(new Intent(mContext, SpecSettingMineActivity.class), Constants.SPEC_SETTING_MINE_REQUEST_CODE);
                }
            }
        });


        userSexGroup.setOnCheckedChangeListener(new OnCheckedChangeListener() {

            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                if (uid == null || "".equals(uid)) {
                    switch (checkedId) {
                        case R.id.id_user_sex_female:
                            mSession.setUserSex(Constants.USER_SEX_FEMALE);
                            break;

                        case R.id.id_user_sex_male:
                            mSession.setUserSex(Constants.USER_SEX_MALE);
                            break;

                        default:
                            break;
                    }
                }
            }
        });
        userSexGroup.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                getActivity().startActivityForResult(new Intent(mContext, SpecSettingMineActivity.class), Constants.SPEC_SETTING_MINE_REQUEST_CODE);
            }
        });
        if (uid == null || "".equals(uid)) {
            userSexGroup.getChildAt(0).setClickable(true);
            userSexGroup.getChildAt(1).setClickable(true);
        } else {
            userSexGroup.getChildAt(0).setClickable(false);
            userSexGroup.getChildAt(1).setClickable(false);
        }
    }

    @Override
    public void onStart() {
        super.onStart();
    }

    @Override
    public void onResume() {
        super.onResume();
        String userPhoto = mSession.getUserIcon();
        if (userPhoto != null && !"".equals(userPhoto)) {
            ImageLoader.getInstance().displayImage(mSession.getUserIcon(), userInfoPhoto, displayImageOptions);
        }

        String userName = mSession.getUserName();
        if (userName == null || "".equals(userName)) {
            userName = "您还未登录";
        }
        userNickName.setText(userName);
        asycTextView.setText(userName);
        MobclickAgent.onPageStart("MainScreen"); //统计页面
    }

    @Override
    public void onPause() {
        super.onPause();
        MobclickAgent.onPageEnd("MainScreen");
    }

    @Override
    public void onStop() {
        super.onStop();
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }

    @Override
    public void onDetach() {
        super.onDetach();
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        if(mSession == null){
            return;
        }
        uid = mSession.getUid();
        if (uid == null || "".equals(uid)) {
            userSexGroup.getChildAt(0).setClickable(true);
            userSexGroup.getChildAt(1).setClickable(true);
        } else {
            userSexGroup.getChildAt(0).setClickable(false);
            userSexGroup.getChildAt(1).setClickable(false);
        }

        String skinType = getString(R.string.user_pre_setting_skin_type_unknow);
        if (requestCode == Constants.SPEC_LOGIN_REQUEST_CODE && resultCode == Constants.SPEC_LOGIN_RESULT_CODE) {
            mContext.startActivity(new Intent(mContext, SpecSettingMineActivity.class));
            ageTextView.setText(mSession.getUserBriday());
            if (Constants.USER_SEX_FEMALE.equals(mSession.getUserSex())) {
                userSexGroup.check(R.id.id_user_sex_female);
            } else if (Constants.USER_SEX_MALE.equals(mSession.getUserSex())) {
                userSexGroup.check(R.id.id_user_sex_male);
            }
            if (Constants.USER_SKIN_TYPE_DRY.equals(mSession.getUserSkinType())) {
                skinType = getString(R.string.user_pre_setting_skin_type_dry);
            } else if (Constants.USER_SKIN_TYPE_OILY.equals(mSession.getUserSkinType())) {
                skinType = getString(R.string.user_pre_setting_skin_type_oil);
            } else if (Constants.USER_SKIN_TYPE_HYBRID.equals(mSession.getUserSkinType())) {
                skinType = getString(R.string.user_pre_setting_skin_type_hybird);
            } else if (Constants.USER_SKIN_TYPE_SENSITIVE.equals(mSession.getUserSkinType())) {
                skinType = getString(R.string.user_pre_setting_skin_type_sensitive);
            } else if (Constants.USER_SKIN_TYPE_UNKNOW.equals(mSession.getUserSkinType())) {
                skinType = getString(R.string.user_pre_setting_skin_type_unknow);
            }
            skinTextView.setText(skinType);
        } else if ((requestCode == Constants.SPEC_SETTING_MINE_REQUEST_CODE && resultCode == Activity.RESULT_OK) || (requestCode == Constants.SPEC_SETTING_LIST_REQUEST_CODE)) {//&&
            String userPhoto = mSession.getUserIcon();
            if (userPhoto != null && !"".equals(userPhoto)) {
                ImageLoader.getInstance().displayImage(mSession.getUserIcon(), userInfoPhoto, displayImageOptions);
            } else {
                userInfoPhoto.setImageResource(R.drawable.ic_tourist);
            }
            String userName = mSession.getUserName();
            if (userName == null || "".equals(userName) || "null".equals(userName)) {
                userName = "您还未登录";
            }
            userNickName.setText(userName);
            ageTextView.setText(mSession.getUserBriday());
            if (Constants.USER_SEX_FEMALE.equals(mSession.getUserSex())) {
                userSexGroup.check(R.id.id_user_sex_female);
            } else if (Constants.USER_SEX_MALE.equals(mSession.getUserSex())) {
                userSexGroup.check(R.id.id_user_sex_male);
            }
            if (Constants.USER_SKIN_TYPE_DRY.equals(mSession.getUserSkinType())) {
                skinType = getString(R.string.user_pre_setting_skin_type_dry);
            } else if (Constants.USER_SKIN_TYPE_OILY.equals(mSession.getUserSkinType())) {
                skinType = getString(R.string.user_pre_setting_skin_type_oil);
            } else if (Constants.USER_SKIN_TYPE_HYBRID.equals(mSession.getUserSkinType())) {
                skinType = getString(R.string.user_pre_setting_skin_type_hybird);
            } else if (Constants.USER_SKIN_TYPE_SENSITIVE.equals(mSession.getUserSkinType())) {
                skinType = getString(R.string.user_pre_setting_skin_type_sensitive);
            } else if (Constants.USER_SKIN_TYPE_UNKNOW.equals(mSession.getUserSkinType())) {
                skinType = getString(R.string.user_pre_setting_skin_type_unknow);
            }
            skinTextView.setText(skinType);
            if (resultCode == Constants.SPEC_SETTING_LIST_RESP_CODE) {
                getActivity().startActivity(new Intent(getActivity(), PreSettingActivity.class));
                getActivity().finish();
            }
        } else if (requestCode == Constants.REQUEST_NO_NET_REQUEST_CODE && resultCode == Activity.RESULT_OK) {
            ageTextView.setText(mSession.getUserBriday());
            if (Constants.USER_SKIN_TYPE_DRY.equals(mSession.getUserSkinType())) {
                skinType = getString(R.string.user_pre_setting_skin_type_dry);
            } else if (Constants.USER_SKIN_TYPE_OILY.equals(mSession.getUserSkinType())) {
                skinType = getString(R.string.user_pre_setting_skin_type_oil);
            } else if (Constants.USER_SKIN_TYPE_HYBRID.equals(mSession.getUserSkinType())) {
                skinType = getString(R.string.user_pre_setting_skin_type_hybird);
            } else if (Constants.USER_SKIN_TYPE_SENSITIVE.equals(mSession.getUserSkinType())) {
                skinType = getString(R.string.user_pre_setting_skin_type_sensitive);
            } else if (Constants.USER_SKIN_TYPE_UNKNOW.equals(mSession.getUserSkinType())) {
                skinType = getString(R.string.user_pre_setting_skin_type_unknow);
            }
            skinTextView.setText(skinType);
        }


        super.onActivityResult(requestCode, resultCode, data);
    }


}
